package com.cloud.pole.shop.controller.goods.admin;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.shop.biz.goods.ShopGoodsBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsAdmin;
import com.cloud.pole.shop.model.vo.goods.GoodsDetailVo;
import com.cloud.pole.shop.service.goods.GoodsService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/goods/goods/admin_merch")
@Slf4j
public class GoodsAdminMerchantController extends BaseController<ShopGoodsBiz,ShopGoods>{
	
	@Autowired
    private GoodsService goodsService;
	
	/**
	 * 分页商品
	 * @param pageNo
	 * @param pageSize
	 * @param mobile
	 * @param templateName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
    @ResponseBody
    public ApiRestResponse<ShopGoodsAdmin> list(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "1") Integer pageNo,
    		@RequestParam(defaultValue = "10") Integer pageSize,
    		@RequestParam(defaultValue = "") String goodsName,
    		@RequestParam(defaultValue = "") String shopId){
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("goodsName", goodsName);
			if(!StringUtils.isEmpty(shopId)) {
				params.put("shopId", shopId);
			}
			PageInfo<ShopGoodsAdmin> pageInfo = goodsService.selectPageMerchantShopGoodsAdminList(token, params, pageNo, pageSize);
			return ApiRestResponse.successOfPage("成功", (int)pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageSize(),
					pageInfo.getList());
		}  catch (BaseException e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@RequestMapping(value = "/getGoodsInfo/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ApiRestResponse<GoodsDetailVo> getOrderInfo(@PathVariable String id){
		try {
			GoodsDetailVo goodsDetail = null;
			String msg = "成功";
			if(id != null){
				goodsDetail = goodsService.selectShopGoodsAdminById(id);
			}else{
				msg = "商品编号不能为空";
			}
			return ApiRestResponse.success(msg, goodsDetail);
		}  catch (BaseException e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	/**
	 * 新增商品
	 * <p>Title: addGoods</p>  
	 * <p>Description: </p>  
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/addGoods",method = RequestMethod.POST)
    @ResponseBody
    public ApiRestResponse<ShopGoods> addGoods(@RequestBody Map map){
		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			goodsService.saveGoods(token,map);
			return ApiRestResponse.success("成功", null);
		} catch (BaseException e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@RequestMapping(value = "/updateGoods/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public ApiRestResponse<ShopGoods> updateGoods(@RequestBody Map map){
		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			goodsService.updateGoods(token,map);
			return ApiRestResponse.success("成功", null);
		}  catch (BaseException e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	/**
	 * 删除商品
	 * <p>Title: deleteGoods</p>  
	 * <p>Description: </p>  
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteGoods/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public ApiRestResponse<String> deleteGoods(@PathVariable int id){
		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			goodsService.deleteGoods(token, id);
			return ApiRestResponse.success("成功", null);
		} 
		catch (BaseException e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} 
		catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
